package com.kehutong.house.app;

import com.kehutong.house.entity.Community;
import com.kehutong.house.entity.Parking;
import com.kehutong.house.entity.ParkingClassify;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;

import java.util.List;

/**
 * 车位信息
 */
@MvcAction
@ReqMapping("/kht-bin/basic/parking")
public class ParkingAction {

    @Inject
    private JdbcSession jdbcSession;


    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
        List<Parking> parkings = jdbcSession.findArray(Parking.class)
                .eq(jsonObject,"id","communityNo", "classNo")
                .eq("deleted", false)
                .order("name","ASC")
                .exe();

        parkings.forEach(item->{
            String name = item.getOwnerName();
            if(Objects.nonEmpty(name)){
                if (name.length() > 2) {
                    item.setOwnerName(name.substring(0, 1) + "*" + name.substring(2));
                }else {
                    item.setOwnerName(name.substring(0, 1) + "*");
                }
            }else{
                item.setOwnerName("物业公司");
            }
        });

        return Result.success(parkings);
    }

    @ReqMapping("/get")
    public Object get(JSONObject jsonObject) {
        Parking parking = Pool.get(Parking.class,  jsonObject.getString("id"));

        String name = parking.getOwnerName();
        if(Objects.nonEmpty(name)){
            if (name.length() > 2) {
                parking.setOwnerName(name.substring(0, 1) + "*" + name.substring(2));
            }else {
                parking.setOwnerName(name.substring(0, 1) + "*");
            }
        }else{
            parking.setOwnerName("物业公司");
        }
        Community community = Pool.get(Community.class, parking.getCommunityNo());

        ParkingClassify parkingClassify = Pool.get(ParkingClassify.class, parking.getClassNo());
        JSONObject result = new JSONObject();
        result.put("parking", parking);
        result.put("community", community);
        result.put("parkingClassify", parkingClassify);

        return Result.success(result);
    }
}
